package com.dao.airticle;

import com.mapper.ArticleMapper;
import com.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LabelDao {

    /**
     *自定义标签
     */
    public static void add_label(String label_name) throws IOException {
        System.out.println("[info]新建标签");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        articleMapper.add_label(label_name);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }

    /**
     * 搜索标签
     */
    public static List<String> search_label(String label_name)throws IOException{
        System.out.println("[info]搜索标签");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        List<String> label_list;
        label_list=articleMapper.search_label(label_name);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
        //返回数据
        return label_list;
    }

    /**
     * 获取当前文章标签
     */
    public static List<String> get_blog_label(String blog_id)throws IOException{
        System.out.println("[info]获取当前文章标签");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        List<String> label_list;
        label_list=articleMapper.get_current_label(blog_id);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
        //返回数据
        return label_list;
    }

    /**
     * 删除博客的标签
     */
    public static void del_blog_label(String blog_id)throws IOException{
        System.out.println("[info]删除博客的标签");
        //获取sqlSession对象
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        //3.获取Mapper接口的代理对象
        ArticleMapper articleMapper=sqlSession.getMapper(ArticleMapper.class);
        //4.执行方法
        articleMapper.del_blog_label(blog_id);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }
}
